package com.zjut.an.news.manager;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.bytecode.Descriptor.Iterator;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.zjut.an.acquisition.dao.AnAcquisition;
import com.zjut.an.acquisition.dao.AnAcquisition.AcquisitionResultType;
import com.zjut.an.acquisition.dao.AnAcquisitionDAO;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistory;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistoryDAO;
import com.zjut.an.news.dao.AnNews;
import com.zjut.an.news.dao.AnNewsDAO;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.webclass.dao.AnWebclass;
import com.zjut.an.webclass.manager.AnWebclassManager;


public class AnNewsManager {

	@Resource(name = "AnNewsDAO")
	private AnNewsDAO dao;
	
	@Resource(name = "AnAcquisitionDAO")
	private AnAcquisitionDAO acquDao;
	
	@Resource(name = "AnAcquisitionHistoryDAO")
	private AnAcquisitionHistoryDAO acquHisDao;
	
	@Resource(name = "AnWebclassManager")
	private AnWebclassManager wmanager;
	
	public void addData(AnNews obj) {	
		dao.save(obj);
	}
	
	public void updateData(AnNews obj) {
		dao.update(obj);
	}
	
	public void deleteData(AnNews obj) {
		dao.delete(obj);
	}

	public void deleteDatas(List<AnNews> objllegeList) {
		for (AnNews AnNews : objllegeList) {
			dao.delete(AnNews);
		}
	}
	
	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order,AnNews obj) {
		//System.out.println("#########"+dao);
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public List getDatas() {
		return dao.findAll();
	}

	public AnNews getData(int objId) {
		return dao.findById(objId);
	}
	
	public PageModule getPubDatasByWcId(int pageNumber,int pageSize, String sort, String order, AnWebclass webclass) {
		AnNews obj = new AnNews();
    	obj.setAnWebclass(webclass);
    	obj.setIscheck(Status.ISCHECK_YES.getStatus());
    	obj.setDel(0);
    	return this.getDataByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public PageModule getPubDatasByTopWcId(int pageNumber,int pageSize, String sort, String order, AnWebclass webclass) {
		String hql = "from AnNews where (1=0 ";
		String hqlc = "select count(*) from AnNews where (1=0 ";
		List wlist = wmanager.getChildrenDatas(webclass.getWcId());
		if (wlist!=null && wlist.size()>0) {
			for (int i=0; i<wlist.size(); i++) {
				hql+=" or anWebclass.wcId="+((AnWebclass)wlist.get(i)).getWcId();
				hqlc+=" or anWebclass.wcId="+((AnWebclass)wlist.get(i)).getWcId();
			}
		}
		hql+=" or anWebclass.wcId="+webclass.getWcId()+" ) and ischeck='"+Status.ISCHECK_YES.getStatus()+"' order by istop desc, adddate desc, place desc, neId desc";
		hqlc+=" or anWebclass.wcId="+webclass.getWcId()+" ) and ischeck='"+Status.ISCHECK_YES.getStatus()+"' ";
		System.out.println(hql);
    	return new PageModule(pageNumber, pageSize, dao.findCountByHql(hqlc, null), dao.findByHql(pageNumber, pageSize, hql, null));
	}
	
	public PageModule getPubPicDatasByTopWcId(int pageNumber,int pageSize, String sort, String order, AnWebclass webclass) {
		String hql = "from AnNews where (1=0 ";
		String hqlc = "select count(*) from AnNews where (1=0 ";
		List wlist = wmanager.getChildrenDatas(webclass.getWcId());
		if (wlist!=null && wlist.size()>0) {
			for (int i=0; i<wlist.size(); i++) {
				hql+=" or anWebclass.wcId="+((AnWebclass)wlist.get(i)).getWcId();
				hqlc+=" or anWebclass.wcId="+((AnWebclass)wlist.get(i)).getWcId();
			}
		}
		hql+=" or anWebclass.wcId="+webclass.getWcId()+" ) and ischeck='"+Status.ISCHECK_YES.getStatus()+"' and picture is not null and picture<>'' order by istop desc, adddate desc, place desc, neId desc";
		hqlc+=" or anWebclass.wcId="+webclass.getWcId()+" ) and ischeck='"+Status.ISCHECK_YES.getStatus()+"' and picture is not null and picture<>'' ";
    	return new PageModule(pageNumber, pageSize, dao.findCountByHql(hqlc, null), dao.findByHql(pageNumber, pageSize, hql, null));
	}
	
	public void saveHit(Map<Integer,Integer> hitMap) throws Exception {
		for(Map.Entry<Integer, Integer> entry: hitMap.entrySet()) {
			String hql = "update AnNews re set re.hit="+entry.getValue()+" where re.neId="+entry.getKey();
			System.out.println(hql);
			dao.excuteHql(hql);
		}
	}
}
